package com.example.hxpc.guidancemanagement.fragment;


import android.app.Fragment;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.hxpc.guidancemanagement.R;
import com.example.hxpc.guidancemanagement.activity.LoginActivity;
import com.example.hxpc.guidancemanagement.api.BaseApi;
import com.example.hxpc.guidancemanagement.model.JsonBase;
import com.example.hxpc.guidancemanagement.model.JsonFeedback;
import com.example.hxpc.guidancemanagement.tools.LogoutT;
import com.example.hxpc.guidancemanagement.tools.MD5Util;
import com.example.hxpc.guidancemanagement.tools.PhoneJudgeUtils;
import com.example.hxpc.guidancemanagement.tools.ProcessingSMSTools;
import com.example.hxpc.guidancemanagement.tools.TimeCount;
import com.example.hxpc.guidancemanagement.tools.ToastTools;
import com.example.hxpc.guidancemanagement.tools.dataTools.OkHttpTools;

import cn.smssdk.SMSSDK;

/**
 * Created by tao on 2017/11/26.
 * 注册Frament
 */

public class RegisterFragment extends Fragment implements View.OnClickListener,BaseApi{
    private View rootView;
    private EditText edt_phone , edt_pass , edt_code ,edt_name;
    private RelativeLayout re_code;
    private Button bu_register;
    private TextView te_sendCode;
    private OkHttpTools okHttpTools = new OkHttpTools();
//    private boolean isSuccess = true;
//    private FragmentManager fmanager;
//    private FragmentTransaction ftransaction;
    private Handler myHandler = new Handler(){
    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        switch (msg.what){
            case 1:
                okHttpTools.register(RegisterFragment.this, getActivity(), edt_name.getText().toString(), edt_phone.getText().toString(), MD5Util.encrypt(edt_pass.getText().toString()));
                break;
            case 2:
                TimeCount timeCountUtil = new TimeCount(getActivity(), 60000, 1000, te_sendCode);
                timeCountUtil.start();
                ToastTools.toastMsg(getActivity(),"获取成功，注意查看");
                break;
            case 3:
                ToastTools.toastMsg(getActivity(),"获取验证码失败,请填写正确的手机号码");
                break;
            case 4:
                ToastTools.toastMsg(getActivity(),"验证码错误");
                break;
        }
    }
};
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        if (rootView == null){
            rootView = inflater.inflate(R.layout.frament_register,null);
        }
        initView(rootView);
        return rootView;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        ProcessingSMSTools.Cancel();
    }

    public void initView(View rootView){
        edt_phone = (EditText) rootView.findViewById(R.id.reg_edit_1);
        setIconSize(R.drawable.phone,edt_phone);
        edt_pass = (EditText) rootView.findViewById(R.id.reg_edit_2);
        setIconSize(R.drawable.register_pass,edt_pass);
        edt_code = (EditText) rootView.findViewById(R.id.reg_edit_3);
        setIconSize(R.drawable.code,edt_code);
        edt_name = (EditText) rootView.findViewById(R.id.reg_edit_4);
        setIconSize(R.drawable.name,edt_name);
        bu_register = (Button) rootView.findViewById(R.id.register);
        bu_register.setOnClickListener(this);
        re_code = (RelativeLayout) rootView.findViewById(R.id.getCode);
        re_code.setOnClickListener(this);
        te_sendCode = (TextView) rootView.findViewById(R.id.senCode);
        te_sendCode.setText("验证码");
    }


    //控制图标的大小
    public void setIconSize(int p , EditText editText){
        Drawable drawable = getResources().getDrawable(p);
        drawable.setBounds(10,0,50,50);
        editText.setCompoundDrawables(drawable,null,null,null);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.register:
                if (edt_name.getText().toString().equals("")){
                    ToastTools.toastMsg(getActivity(),"昵称不能为空");
                }else if(edt_phone.getText().toString().equals("")){
                    ToastTools.toastMsg(getActivity(),"手机号不能为空");
                }else if(edt_pass.getText().toString().equals("")){
                    ToastTools.toastMsg(getActivity(),"密码不能为空");
                }else if (!PhoneJudgeUtils.judgePhoneNums(edt_phone.getText().toString())){
                    ToastTools.toastMsg(getActivity(),"手机号格式错误");
                }else if (edt_code.getText().toString().equals("")){
                    ToastTools.toastMsg(getActivity(),"验证码不能为空");
                }else{
                    SMSSDK.submitVerificationCode("86",edt_phone.getText().toString(),edt_code.getText().toString());
                    ProcessingSMSTools.smsFlage = 2;
                }
                break;
            case R.id.getCode:

                if (edt_phone.getText().toString().equals("")){
                    ToastTools.toastMsg(getActivity(),"请输入手机号");
                }else if (!PhoneJudgeUtils.judgePhoneNums(edt_phone.getText().toString())){
                    ToastTools.toastMsg(getActivity(),"手机号格式错误");
                }else {
                    ProcessingSMSTools.initReceiverCode(myHandler);
                    //获取验证码
                    SMSSDK.getVerificationCode("86",edt_phone.getText().toString());
                    ProcessingSMSTools.smsFlage = 1;
                }
                break;
        }
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden){
            edt_name.setText("");
            edt_phone.setText("");
            edt_pass.setText("");
            edt_code.setText("");
            te_sendCode.setText("验证码");
        }
    }


    @Override
    public void backMsg(String success, JsonBase jsonBase) {
        LogoutT.logout(success + jsonBase.getMsg() + jsonBase.getCode());
        if (success.equals("true")&&jsonBase.getMsg().equals("注册成功")){
            LogoutT.logout("78787878");
            ToastTools.toastMsg(getActivity(),jsonBase.getMsg());
            LoginActivity la = (LoginActivity) getActivity();
            la.showOnFragment();
        }else{
            ToastTools.toastMsg(getActivity(),jsonBase.getMsg());
        }
    }

    @Override
    public void backMsg(String success, JsonFeedback jsonFeedback) {

    }
}
